package com.ruoyi.client.fxiaoke.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ruoyi.client.fxiaoke.beans.args.TextMsgArg;
import com.ruoyi.client.fxiaoke.beans.args.TextMsgArg.Text;
import com.ruoyi.client.fxiaoke.beans.results.TextMsgResult;
import com.ruoyi.client.fxiaoke.exception.AccessTokenException;
import com.ruoyi.client.fxiaoke.service.MessageManager;
import com.ruoyi.client.fxiaoke.utils.OpenAPIUtils;
import com.ruoyi.client.fxiaoke.utils.TokenUtil;
import com.ruoyi.client.utils.TokenModel;
import com.ruoyi.core.api.util.PropertyUtil;
import com.ruoyi.core.util.StringUtil;
import com.ruoyi.platform.model.system.FXiaoKeParameter;

@Service("messageManager")
public class MessageManagerImpl implements MessageManager {

    @Override
    public TextMsgResult sendTextMsg(List<String> openUserIds, String msgContent) throws AccessTokenException {
        TextMsgArg arg = new TextMsgArg();
        arg.setMsgType("text");
        arg.setToUser(openUserIds);
        Text text = arg.new Text();
        text.setContent(msgContent);
        arg.setText(text);
        TokenModel token =TokenUtil.getCorpAccessToken();
        arg.setCorpAccessToken(token.getToken());
        arg.setCorpId(token.getCorpId());
        
        return OpenAPIUtils.sendTextMsg(arg);
    }

	@Override
	public TextMsgResult sendTextMsgToAdmin(String msgContent) throws Exception {
		//获取推送用户
		String adminUser = PropertyUtil.getByAlias(FXiaoKeParameter.FXIAOKE_OPENUSERID,"");
		String users = PropertyUtil.getByAlias(FXiaoKeParameter.FXIAOKE_TO_USER,"");
		List<String> userList = new ArrayList<String>();
		String [] userstr = users.split(",");
		for (int i = 0; i < userstr.length; i++) {
			if(StringUtil.isNotEmpty(userstr[i]))
				userList.add(userstr[i]);
		}
		userList.add(adminUser);
		
		return sendTextMsg(userList,msgContent);
	}

}
